<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/25
 * Time: 16:29
 */

namespace app\admin\controller;

use app\admin\model\About as AboutModel;
use think\Request;


class AboutController extends BaseController
{
    public function lst()
    {
        $data = AboutModel::order('id','asc')->paginate(6);
        $page = $data->render();
        $this->assign([
            'page' => $page,
            'data' => $data,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $controller = strtolower($request->controller());

                $pic = \request()->file('pic');
                if($pic){
                    $info = $pic->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $controller);
                    if($info){
                        $params['pic'] = 'uploads' . DS . $controller . DS . $info->getSaveName();
                    }else{
                        $this->error($pic->getError());
                    }
                }

                $model = new AboutModel();
                if($params['status'] == 1){
                    $model->where('status','eq',1)->setField('status',2);
                }
                if($model->allowField(true)->save($params)){
                    $this->success('添加关于我们成功!',url('admin/about/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function edi()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign('data',AboutModel::find($id));
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new AboutModel();
                $oldInfo = $model->where('id', 'eq', $params['id'])->find();
                $controller = strtolower($request->controller());

                $pic = \request()->file('pic');
                if($pic){
                    $info = $pic->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $controller);
                    if($info){
                        $params['pic'] = 'uploads' . DS . $controller . DS . $info->getSaveName();
                    }else{
                        $this->error($pic->getError());
                    }
                    if(is_file($oldInfo['pic'])){
                        unlink($oldInfo['pic']);
                    }
                }

                if(!empty($params['video'])){
                    if(is_file($oldInfo['video'])){
                        unlink($oldInfo['video']);
                    }
                }

                if($params['status'] == 1){
                    $model->where('status','eq',1)->setField('status',2);
                }
                if($model->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改联系我们成功!',url('admin/about/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function del()
    {
        $id = $this->request->param("id");
        $model = AboutModel::find($id);
        if(is_file('uploads/about/'.$model->pic)){
            unlink('uploads/about/'.$model->pic);
        }
        if(is_file('uploads/about/'.$model->video)){
            unlink('uploads/about/'.$model->video);
        }
        if(AboutModel::destroy($id)){
            return '亲,您已删除成功!^o^';
        }
        return '删除失败...';
    }

    public function receiveVideo()
    {

        $request = Request::instance();
        $controller = strtolower($request->controller());

        $file = request()->file('video');
        if($file){
            //成功移动路到指定路径
            $info = $file->validate(['ext'=>'mp4,ogg,webm'])->move(ROOT_PATH . 'public' . DS . 'uploads'. DS . $controller);
            if($info){
                //返回json数据给前端js
                $message = 'uploads' . DS .$controller . DS . $info->getSaveName();
                return json([$file,'state'=>'success','message'=>$message]);
            }else{
                //上传失败获取错误信息
                return json(['state'=>'error','message'=>$file->getError()]);
            }
        }
    }
}